package linkedlist;

/**
 * 交换 链表正数第 k 个节点和倒数第 k 个节点的值后，
 * 返回链表的头节点（链表 从 1 开始索引）
 */
public class no1721 {
    public ListNode swapNodes(ListNode head, int k) {
        if (head == null || head.next == null){
            return head;
        }
        ListNode cur = head;
        ListNode fast = head.next;
        ListNode slow = head;
        int k2 = k;
        while (k-1 > 0){
            cur = cur.next;
            fast = fast.next;
            k--;
        }
        while (fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        int temp = cur.val;
        cur.val = slow.val;
        slow.val = temp;

        return head;
    }
}
